package com.ruoyi.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.CreateProjectNo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.domain.entity.PhaseDictionary;
import com.ruoyi.project.domain.entity.Project;
import com.ruoyi.project.domain.entity.ProjectParticipan;
import com.ruoyi.project.domain.query.ProjectParticipanQuery;
import com.ruoyi.project.domain.query.ProjectQuery;
import com.ruoyi.project.mapper.ProjectMapper;
import com.ruoyi.project.mapper.ProjectParticipanMapper;
import com.ruoyi.project.service.IPhaseDictionaryService;
import com.ruoyi.project.service.IProjectParticipanService;
import com.ruoyi.project.service.IProjectService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;


@Service
public class ProjectParticipanServiceImpl extends ServiceImpl<ProjectParticipanMapper, ProjectParticipan> implements IProjectParticipanService {

    @Resource
    private ProjectParticipanMapper projectParticipanMapper;
    @Resource
    private IPhaseDictionaryService dictionaryService;

    //分页
    @Override
    public TableDataInfo getPage(ProjectParticipanQuery query) {
        Page<Object> page = PageHelper.startPage(query.getPageNum(),query.getPageSize());
        List<ProjectParticipan> list = projectParticipanMapper.getList(query);
        return new TableDataInfo(list, page.getTotal());
    }

    //新增
    @Override
    public Boolean addInfo(ProjectParticipan project) {
        project.setOperator(SecurityUtils.getLoginUser().getUser().getNickName());

        Long phaseId = project.getPhaseId();

        //销售项目不需要环节信息
        if ("99".equals(String.valueOf(phaseId))){
            return this.save(project);
        }

        if (phaseId == null) {
            throw new RuntimeException("后端接口异常");
        }
        PhaseDictionary byId = dictionaryService.getById(project.getPhaseId());
        project.setPhaseName(byId.getPhaseName());
        //生成唯一编号
       return this.save(project);
    }

    @Override
    public Boolean updateInfo(ProjectParticipan project) {
        project.setOperator(SecurityUtils.getLoginUser().getUser().getNickName());
        return this.updateById(project);
    }

    @Override
    public Boolean deleteInfo(String id) {
        return this.removeById(id);
    }

    @Override
    public ProjectParticipan getInfoById(String id) {
        return this.getById(id);
    }

    @Override
    public List<ProjectParticipan> getList(ProjectParticipanQuery query) {
        LambdaQueryWrapper<ProjectParticipan> wrapper = new QueryWrapper<ProjectParticipan>().lambda()
                .eq(query.getProjectNo()!=null,ProjectParticipan::getProjectNo,query.getProjectNo())
                .eq(query.getPhaseId()!=null,ProjectParticipan::getPhaseId,query.getPhaseId());
        return this.list(wrapper);
    }

    @Override
    public Boolean getPermission(ProjectParticipanQuery query) {
        if (SecurityUtils.hasRole("admin") || SecurityUtils.hasRole("gjadmin") || SecurityUtils.getUserId().equals(100L))
        {
            System.out.println("当前用户有admin角色权限");
            return true;
        }

        SecurityUtils.getLoginUser().getUser().getRoles();
        Long userId = SecurityUtils.getLoginUser().getUserId();
        query.setParticipantId(userId);
        LambdaQueryWrapper<ProjectParticipan> wrapper = new QueryWrapper<ProjectParticipan>().lambda()
                .eq(query.getProjectNo()!=null,ProjectParticipan::getProjectNo,query.getProjectNo())
                .eq(query.getPhaseId()!=null,ProjectParticipan::getPhaseId,query.getPhaseId())
                .eq(query.getParticipantId()!=null,ProjectParticipan::getParticipantId,query.getParticipantId())
                .in(query.getPostIds()!=null,ProjectParticipan::getPostId,query.getPostIds());
        List<ProjectParticipan> list  = this.list(wrapper);
        System.out.println(list.size());
        return !list.isEmpty();
    }

    @Override
    public Boolean getPermissionSaleAndManage(ProjectParticipanQuery query) {
        SecurityUtils.getLoginUser().getUser().getRoles();
        Long userId = SecurityUtils.getLoginUser().getUserId();
        query.setParticipantId(userId);
        LambdaQueryWrapper<ProjectParticipan> wrapper = new QueryWrapper<ProjectParticipan>().lambda()
                .eq(query.getProjectNo()!=null,ProjectParticipan::getProjectNo,query.getProjectNo())
                .eq(query.getPhaseId()!=null,ProjectParticipan::getPhaseId,query.getPhaseId())
                .eq(query.getParticipantId()!=null,ProjectParticipan::getParticipantId,query.getParticipantId())
                .in(query.getPostIds()!=null,ProjectParticipan::getPostId,query.getPostIds());
        List<ProjectParticipan> list  = this.list(wrapper);
        System.out.println(list.size());
        return !list.isEmpty();
    }

    @Override
    public Boolean getPostPermission(ProjectParticipanQuery query) {

        if (SecurityUtils.hasRole("admin") || SecurityUtils.hasRole("gjadmin"))
        {
            System.out.println("当前用户有admin角色权限");
            return false;//如果是管理员 返回false
        }

        SecurityUtils.getLoginUser().getUser().getRoles();
        Long userId = SecurityUtils.getLoginUser().getUserId();
        query.setParticipantId(userId);
        LambdaQueryWrapper<ProjectParticipan> wrapper = new QueryWrapper<ProjectParticipan>().lambda()
                .eq(query.getProjectNo()!=null,ProjectParticipan::getProjectNo,query.getProjectNo())
                .eq(query.getPhaseId()!=null,ProjectParticipan::getPhaseId,query.getPhaseId())
                .eq(query.getParticipantId()!=null,ProjectParticipan::getParticipantId,query.getParticipantId())
                .in(query.getPostIds()!=null,ProjectParticipan::getPostId,query.getPostIds());
        List<ProjectParticipan> list  = this.list(wrapper);
        return !list.isEmpty();
    }

    @Override
    public List<String> getCurrentUserJoinProject() {
        Long userId = SecurityUtils.getLoginUser().getUserId();

// 创建 LambdaQueryWrapper 对象
        LambdaQueryWrapper<ProjectParticipan> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ProjectParticipan::getParticipantId, userId);

// 只查询 projectNo 字段
        queryWrapper.select(ProjectParticipan::getProjectNo);

// 调用 mapper 的 selectObjs 方法，获取指定字段的值
        List<Object> nameObjects = this.baseMapper.selectObjs(queryWrapper);

// 将 Object 类型的列表转换为 String 类型，并添加 "0" 后返回
        List<String> result = new ArrayList<>(nameObjects.stream()
                .map(obj -> (String) obj)
                .toList());

// 添加值 "0"
        result.add("0");

        return result;
    }

    @Override
    public List<ProjectParticipan> getSaleRole(Long saleId) {
       return projectParticipanMapper.getSaleRole(saleId);
    }

    @Override
    public List<ProjectParticipan> getSaleRoleXs(Long saleId) {
        return projectParticipanMapper.getSaleRoleXS(saleId);
    }

    @Override
    public boolean checkSale(ProjectParticipanQuery query) {
        LambdaQueryWrapper<ProjectParticipan> wrapper = new QueryWrapper<ProjectParticipan>().lambda()
                .eq(ProjectParticipan::getProjectNo,query.getProjectNo())
                .in(ProjectParticipan::getPostId,5L);
        List<ProjectParticipan> list = projectParticipanMapper.selectList(wrapper);
        if(list.isEmpty()){
           return false;
        }
        return true;
    }
}
